package com.ict.exception;

import com.ict.api.result.ApiResponse;
import com.ict.commons.exception.IctException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wujinpeng
 * @version 1.0
 * @date 2024/8/23 0:53
 * @description
 */
@RestControllerAdvice
@Slf4j
public class AllExceptionHandler {

    // 处理自定义异常
    @ExceptionHandler(IctException.class)
    public ApiResponse<String> handleCustomException(IctException ex) {
        log.warn("发生自定义异常：{}", ex.getMessage());
        return ApiResponse.error(HttpStatus.BAD_REQUEST.value(),ex.getMessage());
    }

    // 处理运行时异常
    @ExceptionHandler(RuntimeException.class)
    public ApiResponse<String> handleRuntimeException(RuntimeException ex) {
        log.warn("发生运行时异常：{}", ex.getMessage());
        return ApiResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部错误：" + ex.getMessage());
    }

    // 处理所有未捕获异常
    @ExceptionHandler(Exception.class)
    public ApiResponse<String> handleException(Exception ex) {
        log.warn("发生编译时时异常：{}", ex.getMessage());
        return ApiResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知错误：" + ex.getMessage());
    }
}
